package com.linkit.main.setting;

import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.linkit.config.Setting;
import com.linkit.controller.ArticlesHandler;
import com.linkit.main.AbstractPage;
import com.linkit.main.MainActivity;
import com.linkit.main.R;
import com.linkit.main.article.ArticlesLayout;
import com.linkit.utils.Utils;

public class SettingsLayout extends AbstractPage implements OnClickListener {
	private MainActivity main;

	/** Trạng thái activeTabs danh sách xe */
	public static final int TAB_SITE = 0;
	public static final int TAB_CATS = 1;
	public static final int TAB_TYPE = 2;
	private int tabStatus = TAB_SITE;
	private TextView[] tab = new TextView[3];
	
	private ArticlesLayout articlesLayout;

	/** Layout nội dung */
	private RelativeLayout favoriteContent;

	public SettingsLayout(ArticlesLayout articlesLayout) {
		main = MainActivity.getInstance();
		this.articlesLayout = articlesLayout;
	}
	
	/**
	 * Hiện thị danh sách yêu thích
	 * @param controllerId
	 */
	public void init(){
		main.setOnBackPressedImpl(this);
		showPages();
		showProvinceRoutes();
	}

	@Override
	public void setControllerId() {
		//thiết lập ở các giao diện con
	}

	@Override
	protected void setLayout() {
		// layout = new RelativeLayout(main);
		layout = (RelativeLayout) RelativeLayout.inflate(main,
				R.layout.settings, null);
		
		layout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
				LayoutParams.MATCH_PARENT));
	}

	@Override
	public void showHeader() {
		RelativeLayout tabLayout = (RelativeLayout) RelativeLayout.inflate(
				main, R.layout.settings_header, null);
		main.setBackHeader(R.string.setting_title,
				tabLayout);
		
		ImageView doneBtn = (ImageView)tabLayout.findViewById(R.id.settings_header_done);
		doneBtn.setOnClickListener(this);
	}

	@Override
	public void showContent() {
		
		favoriteContent = (RelativeLayout) layout
				.findViewById(R.id.favorite_layout_content);

		TextView provinceRouteTab = (TextView) layout
				.findViewById(R.id.favorite_province_route_tab);
		provinceRouteTab.setOnClickListener(this);
		tab[TAB_SITE] = provinceRouteTab;

		TextView stationRouteTab = (TextView) layout
				.findViewById(R.id.favorite_station_route_tab);
		stationRouteTab.setOnClickListener(this);
		tab[TAB_CATS] = stationRouteTab;

		TextView companyTab = (TextView) layout
				.findViewById(R.id.favorite_company_tab);
		companyTab.setOnClickListener(this);
		tab[TAB_TYPE] = companyTab;
		
		main.setSubContentLayout(layout);
	}

	@Override
	public void onClick(View v) {
		int id = v.getId();
		switch (id) {
		case R.id.favorite_province_route_tab:
			if (tabStatus == TAB_SITE)
				return;
			showProvinceRoutes();
			break;
		case R.id.favorite_station_route_tab:
			if (tabStatus == TAB_CATS)
				return;
			showStationRoutes();
			break;
		case R.id.favorite_company_tab:
			if (tabStatus == TAB_TYPE)
				return;
			showCompanyRoutes();
			break;
		case R.id.settings_header_done:
			ArticlesHandler articlesHandler = new ArticlesHandler(null);
			articlesHandler.doRequest(ArticlesLayout.INIT, Setting.getSiteId(), Setting.getCatsId(), Setting.getTypes(), null);
			break;
		}
	}

	/**
	 * Hiện thị tuyến tỉnh - tỉnh
	 */
	private void showProvinceRoutes() {
		updateActiveTab(TAB_SITE);
		SitesLayout fpRoutesLayout = new SitesLayout();
		fpRoutesLayout.showPages();
		addContentView(fpRoutesLayout.getLayout());
	}

	/**
	 * Hiện thị tuyến Bến - Bến
	 */
	private void showStationRoutes() {
		updateActiveTab(TAB_CATS);
		CategoriesLayout fsRoutesLayout = new CategoriesLayout();
		fsRoutesLayout.showPages();
		addContentView(fsRoutesLayout.getLayout());
	}

	/**
	 * Hiện thị tuyến Hãng
	 */
	private void showCompanyRoutes() {
		updateActiveTab(TAB_TYPE);
		TypesLayout fLayout = new TypesLayout();
		fLayout.showPages();
		addContentView(fLayout.getLayout());
	}

	private void updateActiveTab(int tabIndex) {
		tabStatus = tabIndex;

		if (tabIndex < 0 || tabIndex > 3) {
			return;
		}

		for (int i = 0; i < tab.length; i++) {
			if (tabIndex == i) {
				tab[i].setTextColor(Utils.getColor(R.color.blue_button_main));
				tab[i].setBackgroundResource(R.drawable.favorite_tab_select);
			} else {
				tab[i].setTextColor(Utils.getColor(R.color.dark_main));
				tab[i].setBackgroundColor(Utils.getColor(R.color.bagie_light));
			}
		}

	}

	/**
	 * Thêm view mới vào nội dung của trang cá nhân
	 * 
	 * @param view
	 */
	public void addContentView(View view) {
		favoriteContent.removeAllViews();
		favoriteContent.addView(view);
	}

	@Override
	public void onBackPressed() {
		main.removeSubContentlayout();
		articlesLayout.showHeader();
	}
}
